/**
 * 关键词回复列表
 * @authors 杨兴洲（of2502）
 * @date    2017/5/23 0023 21:05
 * @version 1.0
 * @last-modify  杨兴洲（of2502）
 */

import React, {Component} from 'react';
import {Map} from 'immutable';
import {Relax} from 'iflux2';
import {Pagination} from 'antd';
import KeywordRuleItem from './KwRuleItem';
import NoKwReply from './NoKwReply';
import CreateKwReply from './CreateKwReply';

import GaUtil from 'util/ga-util';

@Relax
export default class KwReplyList extends Component {


    static defaultProps = {
        kw_list: Map(),
        kw_total: 0,
        kw_pageSize: 2,
        kw_page: 1,
        updateKeywordReplyRule: () => void 0,
        fetchKeywordReplyList: () => void 0,
        createKeywordReplyRule: () => void 0,
        deleteKeywordReplyRule: () => void 0,
        getMPNewsDetail: () => void 0,
    };

    constructor(props) {
        super(props);
    }

    componentDidMount() {
        const {kw_page: page, kw_pageSize: pageSize} = this.props;
        this.props.fetchKeywordReplyList({page, pageSize})
    }

    render() {
        const {
            kw_list: list,
            kw_total: total,
            kw_page: page,
            kw_pageSize: pageSize,
            fetchKeywordReplyList,
            getMPNewsDetail
        } = this.props;

        if (total === 0) {
            return (
                <div>
                    <CreateKwReply/>
                    <NoKwReply text="您还没有关键词回复规则哦~"/>
                </div>
            );
        }
        return (
            <div>
                <CreateKwReply/>
                {
                    list.map(item => <KeywordRuleItem
                            key={item.ruleID}
                            rule={item.toJS()}
                            deleteItem={this.deleteItem}
                            changeItem={this.updateItemData}
                            getMPNewsDetail={getMPNewsDetail}
                        />
                    )
                }
                <Pagination
                    current={page}
                    pageSize={pageSize}
                    defaultCurrent={page}
                    defaultPageSize={pageSize}
                    total={total}
                    onChange={index => fetchKeywordReplyList({page: index, pageSize})}/>
            </div>
        )
    }

    deleteItem = async (ruleID) => {
        const {kw_page: page, kw_pageSize: pageSize} = this.props;
        await this.props.deleteKeywordReplyRule(ruleID);
        GaUtil.addEvent({eventAction: "删除关键词回复", eventLabel: "自动回复", eventValue: ruleID});
        // 更新列表
        await this.props.fetchKeywordReplyList({page, pageSize});

    };

    updateItemData = async ({ruleID, rule}) => {
        const {kw_page: page, kw_pageSize: pageSize} = this.props;
        const data = await this.props.updateKeywordReplyRule({ruleID, rule});
        await this.props.fetchKeywordReplyList({page, pageSize});
        GaUtil.addEvent({eventAction: "更新关键词回复", eventLabel: "自动回复", eventValue: ruleID});
        return data;
    };


}